package com.gpy.hospital.reception.controller;

import com.gpy.hospital.cms.pojo.Blog;
import com.gpy.hospital.cms.pojo.Doctor;
import com.gpy.hospital.cms.pojo.Keshi;
import com.gpy.hospital.cms.pojo.LiuYan;
import com.gpy.hospital.cms.service.IBlogService;
import com.gpy.hospital.cms.service.IHuiFuService;
import com.gpy.hospital.cms.service.ILiuYanService;
import com.gpy.hospital.cms.vo.HuiFuVo;
import com.gpy.hospital.common.ServerResponse;
import com.gpy.hospital.reception.service.IDoctorService;
import com.gpy.hospital.reception.service.IKeShiService;
import com.gpy.hospital.reception.service.IZhuanJiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/2/27 18:23
 * @Desc
 **/
@Controller
public class PageController {
    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IKeShiService keShiService;
    @Autowired
    private IBlogService blogService;
    @Autowired
    private ILiuYanService liuYanService;
    @Autowired
    private IHuiFuService huiFuService;
    @Autowired
    private IZhuanJiaService zhuanJiaService;

    @GetMapping("/index")
    public String indexPage(Model model) {
        List<Doctor> doctors = doctorService.getDoctor();
        model.addAttribute("doctors", doctors);
        List<Keshi> keshis = keShiService.getKeShi();
        model.addAttribute("keshis", keshis);
        List<Blog> blogs = blogService.getRandom();
        model.addAttribute("blogs", blogs);
        return "/reception/index";
    }


    @GetMapping("keshi/zhuanjia")
    public String keshiZhuanJian(Model model, @RequestParam("keshiId") Integer keshiId){
        List<Doctor> doctors = doctorService.getByKeShiId(keshiId);
        model.addAttribute("doctors", doctors);
        return "/reception/zhuanija";
    }

    @GetMapping("/reception/liuyan")
    public String liuyanPage() {
        return "/reception/liuyan";
    }

    @GetMapping("/reception/huifu")
    public String huifuPage(Model model){
        List<HuiFuVo> huiFuVoList = huiFuService.getAll();
        model.addAttribute("huiFuVoList", huiFuVoList);
        return "/reception/huifu";
    }


    @PostMapping("/reception/liuyan/add")
    @ResponseBody
    public ServerResponse liuyanAdd(LiuYan liuYan) {
        return liuYanService.add(liuYan);
    }


    @GetMapping("/search")
    public String search(HttpServletRequest request, Model model){
        String type = request.getParameter("Type");
        String searchWords = request.getParameter("SearchWords");
        if (type.equals("2")) {
            List<Doctor> doctors =  zhuanJiaService.getAllByKeyWord(searchWords);
            model.addAttribute("doctors", doctors);
            return "/reception/zhuanijaList";
        }else {
            List<Blog> blogList =  blogService.getAllByTitle(searchWords);
            model.addAttribute("blogList", blogList);
            model.addAttribute("size", blogList.size());
            return "/reception/blogList";
        }
    }

    @GetMapping("/urgent")
    public String urgent() {
        return "/reception/urgent";
    }
}
